package com.itcast.ssm.service.impl;

import com.itcast.ssm.mapper.HouseMapper;
import com.itcast.ssm.pojo.House;
import com.itcast.ssm.pojo.User;
import com.itcast.ssm.service.HouseService;
import com.itcast.ssm.utils.ImageUtil;
import com.itcast.ssm.utils.QiNiuYunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 房屋信息 服务实现类
 * </p>
 *
 * @author 三人行
 * @since 2020-09-12
 */
@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    HouseMapper houseMapper;

    @Resource(name = "qiNiuYunUtil")
    QiNiuYunUtil qiNiuYunUtil;

    @Resource(name = "imageUtil")
    ImageUtil imageUtil;

    @Override
    public String uploadFile(Integer houseId, MultipartFile img) {
        String result = "-1";

        if (houseId != null) {
            House house = houseMapper.findById(houseId);
            String houseImg = house.getHouseImage();
            if(houseImg != null || !houseImg.isEmpty()) {
                //删除原先的图片
                Integer deleteImg = qiNiuYunUtil.deleteImg(houseImg);
            }
        }

        String imgPath = img.getOriginalFilename();
        String imgName = imageUtil.createNewImgName(imgPath);

        try {
            byte[] bytes = img.getBytes();
            result = qiNiuYunUtil.put64image(bytes, imgName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public String insert(House house) {
        String result = "";
        if(house.getHouseName() == null || house.getHouseName().isEmpty()) {
            result =  "房东标题不能为空";
        } else if(house.getHouseArea() == null) {
            result =  "房东房屋面积不能为空";
        }else if(house.getHouseRent() == null) {
            result =  "房东房屋价格不能为空";
        } else if(house.getHouseRent() < house.getHouseMinPrice() || house.getHouseRent() > house.getHouseMaxPrice()) {
            result =  "房东房屋价格应该在房位区间内";
        } else if (house.getHouseImage() == null || house.getHouseImage().isEmpty()) {
            result =  "房东房屋图片不能为空";
        }else {
            result = houseMapper.insert(house) + "";
        }
        return result;
    }

    @Override
    public List<House> findAll() {
        return houseMapper.findAll();
    }

    @Override
    public House findById(Integer houseId) {
        return houseMapper.findById(houseId);
    }

    @Override
    public String update(House house) {
        String result = "";
        if(house.getHouseName() == null || house.getHouseName().isEmpty()) {
            result =  "房东标题不能为空";
        } else if(house.getHouseArea() == null) {
            result =  "房东房屋面积不能为空";
        }else if(house.getHouseRent() == null) {
            result =  "房东房屋价格不能为空";
        } else if(house.getHouseRent() < house.getHouseMinPrice() || house.getHouseRent() > house.getHouseMaxPrice()) {
            result =  "房东房屋价格应该在房位区间内";
        } else if (house.getHouseImage() == null || house.getHouseImage().isEmpty()) {
            result =  "房东房屋图片不能为空";
        }else {
            result = houseMapper.update(house) + "";
        }
        return result;
    }

    @Override
    public Integer delete(Integer houseId) {
        if (houseId != null) {
            House house = houseMapper.findById(houseId);
            String houseImg = house.getHouseImage();
            if(houseImg != null || !houseImg.isEmpty()) {
                //删除原先的图片
                Integer deleteImg = qiNiuYunUtil.deleteImg(houseImg);
            }
        }

        return houseMapper.delete(houseId);
    }
}
